package com.example.message.core;

import java.io.Serializable;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class MessageTemplate implements Serializable{

	private static final long serialVersionUID = 1L;
	private static final Logger LOGGER = LoggerFactory.getLogger(MessageTemplate.class);
	public static final String[] SUPPORTED_LANGUAGES;
	static {
		Locale[] locales = Locale.getAvailableLocales();
		String[] supportedLanguages = new String[locales.length];
		for (int i = 0; i < locales.length; i++) {
			supportedLanguages[i] = locales[i].getLanguage();
		}
		SUPPORTED_LANGUAGES = supportedLanguages;
		LOGGER.debug("Supported languages: " + SUPPORTED_LANGUAGES);
	}

	private String template;
	private String language;
	private String category;
	private String subcategory;
	private String keywords;
	private String author;

	public String format(String[] parameters) {
		String template = getTemplate();
		return String.format(template, (Object[]) parameters);
	}
	
	protected String getTemplate() {
		return template;
	}

	protected void setTemplate(String template) {
		this.template = template;
	}

	public final void setLanguage(String language) {
		for (String supportedLanguage : SUPPORTED_LANGUAGES) {
			if (supportedLanguage.equals(language)) {
				this.language = language;
				return;
			}
		}
		throw new UnsupportedLanguageException(language);
	}

	public String getLanguage() {
		return language;
	}
	
	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public String getSubcategory() {
		return subcategory;
	}

	public void setSubcategory(String subcategory) {
		this.subcategory = subcategory;
	}

	public String getKeywords() {
		return keywords;
	}

	public void setKeywords(String keywords) {
		this.keywords = keywords;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}
}
